#include "linechart.h"
#include <QPainter>

#include <QDebug>

LineChart::LineChart(QWidget *parent) :
    QWidget(parent)
{
    m_curvePixmap = QPixmap(400, 200);
    m_curvePixmap.fill(Qt::black);


    m_curveStartIdx = 0;
    m_curveEndIdx = 9;
    m_curvePoints.append(1);
    m_curvePoints.append(1);
    m_curvePoints.append(0.5);
    m_curvePoints.append(-0.6);
    m_curvePoints.append(-1);
    m_curvePoints.append(-1);
    m_curvePoints.append(16);
    m_curvePoints.append(10);
    m_curvePoints.append(10);
    m_curvePoints.append(20);

    minY = -1;
    maxY = 1;

    drawCurve();
 //   m_curvePixmap.scroll(-150, 0, m_curvePixmap.rect());
}

void LineChart::paintEvent(QPaintEvent *e)
{
    QPainter p(this);


    p.drawPixmap(10, 10, m_curvePixmap);

}

void LineChart::drawCurve()
{
    QPainter p(&m_curvePixmap);
    p.setRenderHint(QPainter::Antialiasing);


    int ptsCnt = m_curveEndIdx - m_curveStartIdx;
    qreal spanX = (double)m_curvePixmap.width() / (double)ptsCnt;
    ptsCnt++;
    qreal spanY = maxY - minY;
    int height = m_curvePixmap.height() - 1;

    QPolygonF ptsPolyline;
    qreal x = 0.0;
    for (int pt = 0; pt < ptsCnt; ++pt) {
        qreal y = height - ((m_curvePoints[pt] - minY) * height) / spanY;
        qDebug() << QPointF(x, y);
        ptsPolyline.append(QPointF(x, y));
        x += spanX;
    }
    p.setPen(Qt::cyan);
    p.drawPolyline(ptsPolyline);
}
